package zut.edu.cn.concertticketmanagementsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import zut.edu.cn.concertticketmanagementsystem.dto.OrderDTO;
import zut.edu.cn.concertticketmanagementsystem.entity.*;
import zut.edu.cn.concertticketmanagementsystem.service.Collectservice;
import zut.edu.cn.concertticketmanagementsystem.service.OrderFormService;
import zut.edu.cn.concertticketmanagementsystem.service.Ticketservice;
import zut.edu.cn.concertticketmanagementsystem.service.UserService;

import java.util.*;

@Controller

@Slf4j
public class TicketController {

    @Autowired
    private Ticketservice ticketservice;
    @Autowired
    private OrderFormService orderFormService;
    @Autowired
    private UserService userService;
    @Autowired
    private Collectservice collectservice;

    /*展示所有票*/
    @RequestMapping("lmh")
    public String show(Model model,String username) {
        if(username.equals("admin")){
            return "redirect:/admin";
        }
        else {
            List<Ticket> list = ticketservice.findAll();
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).toString());
            }
            model.addAttribute("ticket", list);
            model.addAttribute("username", username);
            System.out.println(username + "AAAAAA");
            return "showmain/tmain";
        }
    }

    /*买票*/
    @RequestMapping("/buyticket")
    public String buyticket(int t_id,String username,double price){
        User user=new User();
        List<User> users=new ArrayList<>();
        users = userService.findUserByusername(username);
        if(users.size()==0)
            return "login";
        else {
            user = users.get(0);
            System.out.print(users.get(0).toString());
            List<OrderDetail> orderDetailList = new ArrayList<>();
            OrderDetail od = new OrderDetail();
            OrderDTO o = new OrderDTO();
            o.setBuyerName(user.getUsername());
            o.setBuyerPhone(user.getPhone());
            String tt_id = String.valueOf(t_id);
            od.setProductId(tt_id);
            od.setProductQuantity(1);
            orderDetailList.add(od);
            o.setOrderDetailList(orderDetailList);
            orderFormService.creat(o);
            return "showmain/2";
        }
    }

    /*收藏*/
    @RequestMapping("/collectticket")
    public String collectticket(int t_id,int u_id){
        Collect collect=new Collect();
        collect.setPid(t_id);
        collect.setUserid(u_id);
        collect.setCtype(0);
        collectservice.create(collect);
        return "collect/albums";
    }

    /* 根据歌手名查看演唱会*/

    @RequestMapping("/findbysinger")
    public String findticketbysinger(String singername,String username,Model model){
        List<Ticket> tickets=new ArrayList<>();
        tickets=ticketservice.queryAllbysingername(singername);
        model.addAttribute("ticket",tickets);
        model.addAttribute("username",username);
        return "showmain/tmain";
    }

    /* 根据地点选择可看的演唱会*/

    @RequestMapping("/findbyaddress")
    public String findbyaddress(String address,String username,Model model){
        List<Ticket> tickets=new ArrayList<>();
        tickets=ticketservice.queryAllbyaddress(address);
        model.addAttribute("ticket",tickets);
        model.addAttribute("username",username);
        return "showmain/tmain";
    }

    @RequestMapping("/admin")
    public String admin(String username,Model model){
        List<Ticket> list = ticketservice.findAll();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).toString());
        }
        model.addAttribute("ticket", list);
        model.addAttribute("username", "admin");
        System.out.println(username + "ADMINADMIN");
        return "showmain/showadmin";
    }

    @RequestMapping("/addticket")
    public String addticket(Model model,String addcount,int t_id){

          int count=Integer.parseInt(addcount);
          System.out.println("LLL"+count);
          ticketservice.increaseStock(t_id,count);
          return "redirect:/admin";
    }

    @RequestMapping("/deleteticket")
    public String delete(Model model,String deletecount,int t_id){
        int count=Integer.parseInt(deletecount);
        ticketservice.decreaseStock(t_id,count);
        return "redirect:/admin";
    }

    @RequestMapping("/adticket")
    public String adticket()
    {
        return "showmain/add";
    }
    @RequestMapping("/add")
    public String add(String t_name,String t_address,String t_singer, String t_price,String t_count,String t_date,String t_starttime,String t_endtime,String t_img,String t_type ){
        if(t_name.equals("")||t_address.equals("")||t_singer.equals("")||t_price.equals("")||t_count.equals("")||t_endtime.equals("")||t_img.equals("")||t_type.equals("")||t_date.equals("")||t_starttime.equals("")){
            return "redirect:/admin";
        }
        Ticket ticket=new Ticket();
        ticket.setT_name(t_name);
        ticket.setT_address(t_address);
        ticket.setT_singername(t_singer);
        int count=Integer.parseInt(t_count);
        ticket.setT_remaincount(count);
        ticket.setT_count(count);
        ticket.setT_type(t_type);
        ticket.setT_img(t_img);
        ticket.setT_endtime(t_endtime);
        ticket.setT_starttime(t_starttime);
        ticket.setT_date(t_date);
        ticket.setT_bill(null);
        double price=Double.valueOf(t_price);
        ticket.setT_price(price);
        ticketservice.save(ticket);
        return "redirect:/admin";

    }
    @RequestMapping("/tdelete")
    public String delete(int t_id){
        ticketservice.deleteByT_id(t_id);
        return "redirect:/admin";
    }
}


